﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FormTest
{
    public static class Ext
    {
        private struct RECT
        {
            public int Left;
            public int Top;
            public int Right;
            public int Bottom;
        }
        // Windows API 相关定义
        public const int LVM_GETHEADER = 0x1000 + 31;
        private const int HDM_GETITEMRECT = 0x1200 + 7;

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);

        // 重载SendMessage以支持ref参数
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, ref RECT lParam);
        public static Rectangle GetColumnHeaderRect(this ListView listView, int columnIndex)
        {
            // 验证列索引有效性
            if (columnIndex < 0 || columnIndex >= listView.Columns.Count)
                return Rectangle.Empty;

            // 通过发送 LVM_GETHEADER 消息获取头部控件句柄
            IntPtr headerHandle = SendMessage(listView.Handle, LVM_GETHEADER, IntPtr.Zero, IntPtr.Zero);
            if (headerHandle == IntPtr.Zero)
                return Rectangle.Empty;

            // 获取列头的矩形区域（使用标准的SendMessage函数）
            RECT rect = new RECT();
            IntPtr result = SendMessage(
                headerHandle,
                HDM_GETITEMRECT,
                (IntPtr)columnIndex,
                ref rect
            );

            if (result == IntPtr.Zero)
                return Rectangle.Empty;

            // 转换为 .NET 的 Rectangle 结构
            return new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
        }
    }





    public class MixedControl : Control
    {
        //public Label StaticLabel { get; } = new Label();
        public TextBox EditTextBox { get; } = new TextBox();
        public string StaticText { get; set; }  // 静态文本部分

        public MixedControl()
        {
            // 设置布局为水平排列
            //StaticLabel.Dock = DockStyle.Left;
            EditTextBox.Dock = DockStyle.Fill;
            EditTextBox.BorderStyle = BorderStyle.FixedSingle;

            // 添加控件
            //Controls.Add(StaticLabel);
            Controls.Add(EditTextBox);
        }
    }
}
